/***************************************************************************
 *   Copyright (C) 2007 by netwan   *
 *   root@double_link   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <QDialog>
#include <QListWidgetItem>
#include <QTreeWidgetItem>
#include <QSettings>
#include "nicform.h"
#include <pcap.h>
#include <QWidget>

#include <QStandardItemModel>
extern int g_NICIndex;
extern char * iptos(u_long in);
extern u_long iptoul(const ip_address ipaddress);

TNicDialog1::TNicDialog1()
{
    ui.setupUi(this);
    QCoreApplication::setOrganizationName("adm soft");
    QCoreApplication::setOrganizationDomain("wanguangming.com");
    QCoreApplication::setApplicationName("neteye");
    
	QObject::connect(ui.pushButton_Cancel,SIGNAL(clicked()), this,SLOT(close()));
    QObject::connect(ui.treeView, SIGNAL(clicked(QModelIndex)),
         this, SLOT(treeView_itemclicked(QModelIndex)));
    QObject::connect(ui.pushButton_Ok, SIGNAL(clicked()),
         this, SLOT(pushButton_Ok_clicked()));
    QStandardItemModel *model = new QStandardItemModel(0, 4, this);
	model->setHeaderData(0, Qt::Horizontal, QObject::trUtf8("nic name"));
	model->setHeaderData(1, Qt::Horizontal, QObject::trUtf8("ip"));
	model->setHeaderData(2, Qt::Horizontal, QObject::trUtf8("netmask"));
	model->setHeaderData(3, Qt::Horizontal, QObject::trUtf8("link"));

	ui.treeView->setModel(model);

    initnic();
    QSettings settings;
    select_nic_index=settings.value("ActiveNIC",0).toInt();
    if(select_nic_index>=model->rowCount())
    {
        select_nic_index = 1;                                       
    }
    QModelIndex index=model->index(select_nic_index-1, 0);
    ui.treeView->setCurrentIndex(index);
    treeView_itemclicked(index); 
}
void TNicDialog1::treeView_itemclicked(const QModelIndex & index)
{
     QStandardItemModel *model = (QStandardItemModel*)ui.treeView->model();
     QStandardItem *item = model->item(index.row());
     //ui.lineEdit->setText(item->text());
     ui.lineEdit->setText("test");
     select_nic_index = index.row()+1;
}
void TNicDialog1::pushButton_Ok_clicked()
{
     QSettings settings;
     settings.setValue("ActiveNIC", select_nic_index);
     g_NICIndex = select_nic_index;
     this->close();
}
/*
*初始化网卡 
*/
int TNicDialog1::initnic()
{
  pcap_if_t *alldevs;
  pcap_if_t *d;
  char errbuf[PCAP_ERRBUF_SIZE];
 
  /* Retrieve the interfaces list */
  if (pcap_findalldevs( &alldevs, errbuf) == -1)
  {
	ShowError(QString("Error in pcap_findalldevs,errorinfo is: %s\n").arg(errbuf));
  }

  /* Scan the list printing every entry */
  for(d=alldevs;d;d=d->next)
  {
	ifprint(d);
  }

  pcap_freealldevs(alldevs);

  return 1;
}

//---------------------------------------------------------------------------
/* Print all the available information on the given interface */
void TNicDialog1::ifprint(pcap_if_t *d)
{
  QStandardItemModel *model = (QStandardItemModel*)ui.treeView->model();
  QStandardItem* pitem=model->invisibleRootItem();
  int irow_count=pitem->rowCount();
  pcap_addr_t *a;
  //char ip6str[128];
  /* Name */


  QStandardItem *item = new QStandardItem();
  
  item->setText("n/a");
  model->setItem(irow_count,0,item);
  model->setItem(irow_count,1,new QStandardItem(QString("0.0.0.0")));
  model->setItem(irow_count,2,new QStandardItem(QString("255.255.255.255")));
  model->setItem(irow_count,3,new QStandardItem(QString("n/a")));
// // // //   li->SubItems->Add("n/a");
  //int i= li->SubItems->Count;
  // Description 
  if (d->name){
	char l_name[255];
	if(d->description)
		sprintf(l_name,"%s(%s)",d->name,d->description);
	else
		sprintf(l_name,"%s(%s)",d->name,"No description available");

	item->setText(l_name);
	//this->WxListCtrl1->InsertItem(i,d->description);
	/*if(li->Index ==select_nic_index) //当前选择了这个适配器
    {
		li->Selected = true;
		//this->WxListCtrl1->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
    }*/
  }

  // Loopback Address
  //printf("\tLoopback: %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no");

  // IP addresses 
  for(a=d->addresses;a;a=a->next)
  {
    //printf("\tAddress Family: #%d\n",a->addr->sa_family);

	switch(a->addr->sa_family)
    {
      case AF_INET:
		model->setItem(irow_count,3,new QStandardItem(QString("AF_INET")));
		//this->WxListCtrl1->SetItem(i,3,"AF_INET");
        if (a->addr)
		  //printf("\tAddress: %s\n",iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr));
		  model->setItem(irow_count,1,new QStandardItem(QString(iptos(ntohl(((struct sockaddr_in *)a->addr)->sin_addr.s_addr)))));
		  //this->WxListCtrl1->SetItem(i,1,iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr));
		if (a->netmask)
		  //printf("\tNetmask: %s\n",iptos(((struct sockaddr_in *)a->netmask)->sin_addr.s_addr));
		  model->setItem(irow_count,2,new QStandardItem(QString(iptos(ntohl(((struct sockaddr_in *)a->netmask)->sin_addr.s_addr)))));
		if (a->broadaddr)
          //printf("\tBroadcast Address: %s\n",iptos(((struct sockaddr_in *)a->broadaddr)->sin_addr.s_addr));
        if (a->dstaddr)
          //printf("\tDestination Address: %s\n",iptos(((struct sockaddr_in *)a->dstaddr)->sin_addr.s_addr));
        break;

      case AF_INET6:
        //printf("\tAddress Family Name: AF_INET6\n");
        if (a->addr)
          //printf("\tAddress: %s\n", ip6tos(a->addr, ip6str, sizeof(ip6str)));
       break;

      default:
        //printf("\tAddress Family Name: Unknown\n");
        break;
    }

  }

}

